<?php
class ModelPrintCategory extends MVC_MODEL {
	/**
	 * @var object SQL
	 */
	public $oDB;
	private $rsListSiteMap ;
	private $test_id;
        
	
	public function __construct(){
		parent::__construct();
		$this->oDB = new BDPrintCategory();
		$this->rsListSiteMap=array();

	}
	public function getPathID4Cat($catID){
		$i=0;
		$arrID=array();
		$catRow=$this->oDB->getInfoCategory($catID);
		while($catRow['product_category_parent_id']!=0){
			$catID=$catRow['product_category_id'];
			$arrID[$i]=$catID;
			$i++;
			$catRow=$this->oDB->getInfoCategory($catRow['product_category_parent_id']);
		}
		$count=count($arrID);
		//print_r($arrID);
		if($count>3){
			$a['brandID']=$arrID[$count-3];
			$a['childID']=$arrID[0];
			return $a;	
		}
		return NULL;
	}
	public function getBrands4Category($name){

		return $brands=array();
		$i=0;
		$NameCats=$this->oDB->getCategoryByName($name);
		$allBrands=$this->oDB->selectAllBrand();
		$allBrandsID=array();
		$j=0;
		foreach($allBrands as $brand){
			$allBrandsID[$j]=$brand['product_category_id'];
			$j++;
		}

		if(is_array($NameCats)){
			foreach($NameCats as $rowCat){
				$brand=$this->getPathID4Cat($rowCat['product_category_id']);
				//print_r($this->oDB->getInfoCategory($brandID));
				if(in_array($brand['brandID'],$allBrandsID)){
					$brands[$i]['brandID']=$brand['brandID'];
					$brands[$i]['childID']=$brand['childID'];
					$i++;
				}
			}
		}
		//print_r($this->oDB->getInfoCategory('2671'));
		//print_r($this->oDB->getInfoCategory('2670'));
		return $brands;
	}
	
	public function getLeftCategory(){
		return $this->oDB->getLeftCategory();
	}
	
	public function getTopCategory(){
		return $this->oDB->getTopCategory();
	}
	
	public function getInfoCategory($id)
	{
		return $this->oDB->getRecordsetByPK($id);
	}
	
	public function getInfoCategoryByYahooID($YID){
		return $this->oDB->getInfoCategoryByYahooID($YID);
	}
	public function deleteCategory($id)
	{
		return $this->oDB->doDeleteByPK($id);
	}
	
	/**
	 * 
	 */
	public function getListCategoryParentChild()
	{
		$rsParent = $this->getListParentCategory();
		$this->setData('category_level',0);
		if(!$rsParent){
			return false;
		}else
		
		{
			foreach ($rsParent as $rowParent)
			{
				$this->setData('category_level',1);
				if($rowParent['product_category_id'] == $this->getData('product_category_id')){
					continue;
				}
				$rowParent['category_level'] = $this->getData('category_level');
				$this->rsListSiteMap[] = $rowParent;
				$this->getListChildCategory($rowParent['product_category_id']);
			}
		}
		
		return $this->rsListSiteMap;
	}
	public function checkExistID($ID,$owner_id){
		return $this->oDB->checkExistID($ID,$owner_id);
	}
	public function checkParentID($ID){
		return $this->oDB->checkParentID($ID);
	}
	public function getListParentCategory(){
		return $this->oDB->getListParentCategory($this);
	}
	public function getListChildCategory($iParent_id)
	{
		$rsChild = $this->oDB->getListChildCategory($iParent_id);	
		if(!$rsChild)
		{
			$this->setData('category_level', $this->getData('category_level')-1);
			return false;
		}else
		{
			$iNumChild = count($rsChild);
			$i=0;
			foreach ($rsChild as $rowChild){
				$i++;
				$this->setData('category_level', $this->getData('category_level')+1);

				if($rowChild['product_category_parent_id'] == $this->getData('product_category_id') || $rowChild['product_category_id'] == $this->getData('product_category_id')){
					break;
				}
				$rowChild['category_level'] = $this->getData('category_level');
				$this->rsListSiteMap[] = $rowChild;
				
				$is_not_left_node = $this->getListChildCategory($rowChild['product_category_id']);
				if(!$is_not_left_node && $i == $iNumChild){
					$this->setData('category_level', $this->getData('category_level')-1);
				}
			}
		}
	}
	
	public function getListChildOnly($iParent_id)
	{
		return $this->oDB->getListChildCategory($iParent_id);	
	}
	
	public function getLastestNumberOfProduct($owner_id)
	{
		return $this->oDB->getLastestNumberOfProduct($this,$owner_id);
	}

		/**
	 * PROCESS IMAGE
	 */
	 public function deleteImage($sImage){
		$sPathMain = PATH_UPLOAD . "category/";
		$sPathThumb120 = $sPathMain . "thumb_20/";
		//die($sPathMain . $sImage);
		@unlink($sPathMain . $sImage);
		@unlink($sPathThumb120 . $sImage);
	}
	
	public function deleteThumbImage($sImage)
	{
		$sPathMain = PATH_UPLOAD . "category/";
		$sPathThumb120 = $sPathMain . "thumb_20/";
		//die($sPathMain . $sImage);
		//@unlink($sPathMain . $sImage);
		@unlink($sPathThumb120 . $sImage);
	}
	
	public function getFullImage($sImage){
		$sPathMain = PATH_UPLOAD . "category/";
		$fileImage = $sPathMain . $sImage;
		
		if(is_file($fileImage)){
			return URL_UPLOAD . "category/" . $sImage;
		}
		return URL_UPLOAD . "wrong.png";
	}
	
	public function getThumbImage($sImage, $iThumbWidth, $iThumbHeight){
		$sPathMain = PATH_UPLOAD . "category/";
		$sPathThumb = "thumb_{$iThumbWidth}/";
		
		if(is_file($sPathMain . $sImage)){
			if(!is_file($sPathMain . $sPathThumb . $sImage)){
				$oThumb = new  thumbnail();
				$oThumb->setPathMain($sPathMain);
				$oThumb->setImage($sImage);
				
				$oThumb->setThumb($sPathThumb . $sImage);
				$oThumb->makeThumb($iThumbHeight, $iThumbWidth, 'resizewscale');
			}
			
			if(is_file($sPathMain . $sPathThumb . $sImage)){
				return URL_UPLOAD . "category/" . $sPathThumb . $sImage;
			}else{
				/**
				 * donnot create image thumbnail, because
				 * image' width less than image thumbnail
				 */
				return URL_UPLOAD . "category/" . $sImage;
			}
		}
		return URL_TEMPLATE_ADMIN . "image/tree_img/empty.gif";	
	}
	/**
	 * CHOOSE CATEGORY
	 */
	 public function getListParentCategoryChoose()
	 {
	 	return $this->oDB->getListParentCategoryChoose($this);
	 }
	 public function getListChildCategoryChoose($parent_id)
	 {
	 	return $this->oDB->getListChildCategoryChoose($parent_id);
	 }
	 public function doInsertGenCategoryId($gen_pro_id,$cat_id,$owner_id)
	 {
	 	return $this->oDB->doInsertGenCategoryId($gen_pro_id,$cat_id,$owner_id);
	 }
	 
	 //UPDATE ALL CHILD
	 public function doUpdateAllChildStatus($parent_id)
	 {
	 	$this->$test_id=null;
	 	$this->getAllChild($parent_id);
	 }
	 
	public function getAllChild($parent_id)
    {
    		$rs=$this->oDB->getListChildCategory($parent_id);
    		if(is_array($rs))
    		{	
    			foreach($rs as $row)
    			{	
    				$cat_id=$row['product_category_id'];
    				$parent_id=$row['product_category_parent_id'];
    				if ($_POST['product_category_status'] == 'on')
               			 $status= 1;
            		else
               			 $status=0;
               		if($this->$test_id===$row['product_category_id'])
               		{
               			break;
               		}
               		else
               		{
               			$this->$test_id=$row['product_category_id'];		
               			$this->oDB->doUpdateAllChildStatus($parent_id,$status);
               		}
    			
					$this->getAllChild($row['product_category_id']);
    			}
    		}
    }
    public function getAllShopOwner(){
        return $this->oDB->getAllShopOwner();
    }
    public function setDefaultCategory($id){
        $this->oDB->default_category_id = $id;
    }

    public function getUserInfo($userID){
        return $this->oDB->getUserInfo($userID);
    }
}
?>